package ast

import "fmt"

type SimpleASTNode struct {
	parent   *SimpleASTNode
	children []*SimpleASTNode
	nodeType Type
	text     string
}

func NewSimpleASTNode(nodeType Type, text string) *SimpleASTNode {
	return &SimpleASTNode{
		text:     text,
		nodeType: nodeType,
	}
}

func (s *SimpleASTNode) GetParent() *SimpleASTNode {
	return s.parent
}

func (s *SimpleASTNode) GetChildren() []SimpleASTNode {
	_readonlyChildren := make([]SimpleASTNode, 0, len(s.children))

	for _, value := range s.children {
		_readonlyChildren = append(_readonlyChildren, *value)
	}
	return _readonlyChildren
}

func (s *SimpleASTNode) GetType() Type {
	return s.nodeType
}

func (s *SimpleASTNode) GetText() string {
	return s.text
}

func (s *SimpleASTNode) AddChild(child *SimpleASTNode) {
	s.children = append(s.children, child)
	child.parent = s
}

func (s SimpleASTNode) String() string {
	return fmt.Sprintf("%s : %s", s.nodeType, s.text)
}

func Dump(s SimpleASTNode, index int) string {
	_str := "|-" // 初始化

	// 拼接缩进
	for i := 0; i < index; i++ {
		_str += "--"
	}
	// 拼接自身
	_str += fmt.Sprintln(s)

	// 拼接子串
	for i := 0; i < len(s.children); i++ {
		_str += Dump(*s.children[i], index+1)
	}
	return _str
}
